$(function () {
    layui.form.verify({
        article:[
            /^[\S]{1,20}$/,
            '不能有空字符'
        ]
    })

    initArtCateList()

    function initArtCateList(){
       axios({
           url:'/my/article/cates',
           method:'get'
       }).then(({data:res})=>{
         if(res.status!==0){
             return layer.msg(res.message)
         }
         let htmlArr=[]
         res.data.forEach(value => {
             htmlArr.push(`
               <tr>
                        <td>${value.Id}</td>
                        <td>${value.name}</td>
                        <td>${value.alias}</td>
                        <td>
                        <button type="button" class="layui-btn layui-btn-xs edit" data-id="${value.Id}">编辑</button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger del" data-id="${value.Id}">删除</button>
                        </td>
                    </tr>
             `)
         })
        $('tbody').empty().html(htmlArr.join(''))
       })
    }

    let addindex=0

    $('#add').on('click', function () {
        addindex=layer.open({
            type: 1,
            title: '添加文章分类',
            skin: 'layui-layer-lan',
            area: ['500px', '260px'],
            content: `
                <form class="layui-form" id="addForm">
        <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
        <input type="text" name="name" required  lay-verify="required|article" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">分类别名</label>
        <div class="layui-input-block">
        <input type="text" name="alias" required  lay-verify="required|article" placeholder="请输入分类别名" autocomplete="off" class="layui-input">
        </div>
        </div>
         <div class="layui-form-item">
        <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
        </div>
                </form>
            `
        })
    })

    $('body').on('submit','#addForm',function(e){
        e.preventDefault()
        axios({
            url:'/my/article/addcates',
            method:'POST',
            data: $(this).serialize()
        }).then(({data:res})=>{
           if(res.status!==0){
               return layer.msg(res.message)
           }
            layer.msg(res.message)
            layer.close(addindex)
            initArtCateList()
        })
    })

     let editindex=0

    $('body').on('click','.edit',function(){
        editindex = layer.open({
            type: 1,
            title: '修改文章分类',
            skin: 'layui-layer-lan',
            area: ['500px', '260px'],
            content: `
                <form class="layui-form" id="editForm" lay-filter="formTest">
        <input type="hidden" name="Id">
        <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
        <input type="text" name="name" required  lay-verify="required|article" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
        </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">分类别名</label>
        <div class="layui-input-block">
        <input type="text" name="alias" required  lay-verify="required|article" placeholder="请输入分类别名" autocomplete="off" class="layui-input">
        </div>
        </div>
         <div class="layui-form-item">
        <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
        </div>
                </form>
            `
        })
        let id=$(this).attr('data-id')
        axios({
            url:`/my/article/cates/${id}`,
            method:'get'
        }).then(({data:res})=>{
           if(res.status!==0){
               return layer.msg(res.message)
           }
            layui.form.val("formTest",res.data)
        })
    })

    $('body').on('submit','#editForm',function(e){
        e.preventDefault()
        axios({
            url:'/my/article/updatecate',
            method:'POST',
            data: $(this).serialize()
        }).then(({data:res})=>{
            if(res.status!==0){
                return layer.msg(res.message)
            }
            layer.msg(res.message)
            layer.close(editindex)
            initArtCateList()
        })
    })

    $('body').on('click','.del',function(){
        let id=$(this).attr('data-id')
        layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
            axios({
                url: `/my/article/deletecate/${id}`,
                method: 'get'
            }).then(({ data: res }) => {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                layer.msg(res.message)
                initArtCateList()
            })
            layer.close(index)
        })
        
    })
})